package de.baumann.browser.activitys.node

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.adapter.NodeListAdapter
import de.baumann.browser.adapter.NodeMemberListAdapter
import de.baumann.browser.api.net.vo.Node
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.INodeListView
import de.baumann.browser.present.NodeListPresenter
import de.baumann.browser.utils.ActiveUtil
import de.baumann.browser.utils.getBoolean
import de.baumann.browser.utils.put
import kotlinx.android.synthetic.main.activity_node_list.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 *@author:
 *@date:2019/9/24 16:14
 *@discription: 节点列表
 */
class NodeListActivity : BaseOdinActivity<INodeListView, NodeListPresenter>(), INodeListView {

    override fun createPresenter(): NodeListPresenter {
        return NodeListPresenter()
    }

    override fun createView(): INodeListView {
        return this
    }

    private lateinit var nodeListAdapter: NodeListAdapter

    companion object {
        fun startNodeListActivity(context: Context) {
            context.startActivity(Intent(context, NodeListActivity::class.java))
        }

        fun startNodeListActivityForDisplay(context: Activity, reCode: Int) {
            context.startActivityForResult(Intent(context, NodeListActivity::class.java).apply {
                putExtra("showCreateButton", false)
            }, reCode)
        }
    }

    override fun getTitleText(): String {
        return "超级节点列表"
    }

    override fun getLayout(): Int {
        return R.layout.activity_node_list
    }

    override fun onRestart() {
        super.onRestart()
        if (getBoolean("isJoined")) {
            put("isJoined", false)
            finish()
        }
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        rvSuperNodeList.layoutManager = LinearLayoutManager(mContext)
        nodeListAdapter = NodeListAdapter(R.layout.item_node_card, intent.getBooleanExtra("showCreateButton", true))
        rvSuperNodeList.adapter = nodeListAdapter

        nodeListAdapter.setOnItemClickListener { adapter, _, position ->

            val node = adapter.getItem(position) as Node

            if(ActiveUtil.superNodeIsClose()){
                NodeMemberActivity.startNodeMemberActivity(mContext,node.nodeId)
                return@setOnItemClickListener
            }

            if (!intent.getBooleanExtra("showCreateButton", true)) {
                setResult(Activity.RESULT_OK, intent.apply {
                    putExtra("nodeId", node.nodeId)
                })
                finish()
            } else {
                NodeStatusActivity.startNodeStatusActivity(mContext, node.nodeId)
            }
        }

        nodeListAdapter.setJoinNodeListener {
            JoinNodeActivity.startJoinNodeActivity(mContext, it)
        }

        if (intent.getBooleanExtra("showCreateButton", true)) {
            btnCreateNode.visibility = View.VISIBLE
        }

        btnCreateNode.setOnClickListener {
            CreateNodeRuleActivity.startCreateNodoRuleActivity(mContext)
        }
    }

    override fun initData() {
        presenter.getNodeList()
    }

    override fun setSuperNodeList(nodes: List<Node>?) {
        nodeListAdapter.setNewData(nodes)
    }
}
